package com.example.mybatisplus.web.controller;

import com.example.mybatisplus.mapper.HOrderMapper;
import com.example.mybatisplus.model.domain.HOrder;
import com.example.mybatisplus.model.dto.DataDto;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.HouseDetailService;
import com.example.mybatisplus.model.domain.HouseDetail;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;


/**
 *
 *  前端控制器
 *
 *
 * @author yfy
 * @since 2022-01-10
 * @version v1.0
 */
@RestController
@RequestMapping("/api/houseDetail")
public class HouseDetailController {

    private final Logger logger = LoggerFactory.getLogger( HouseDetailController.class );

    @Autowired
    private HouseDetailService houseDetailService;
    @Autowired
    private HOrderMapper orderMapper;
    @GetMapping("/show")
    public JsonResponse showAll(){
        return JsonResponse.success(houseDetailService.list());
    }
    @GetMapping("/orderNow")
    public JsonResponse orderNow(DataDto dataDto){
        int times= (int) ChronoUnit.DAYS.between(dataDto.getTime1(), dataDto.getTime2());
        BigDecimal total=dataDto.getBPrice().multiply(BigDecimal.valueOf(times));
        HOrder order = new HOrder().setHId(dataDto.getHId()).setOutId(null).setUId(dataDto.getUId()).setDId(dataDto.getBId()).setOState(1).setUsername(dataDto.getName()).setPState(0).setInState(0).setOutState(0).setBack(null).setInNum(dataDto.getInNum()).setInTime(dataDto.getTime1()).setOutTime(dataDto.getTime2()).setOrTime(LocalDateTime.now()).setPrice(total).setCId(null);
        orderMapper.insert(order);
        return JsonResponse.success(order);
    }
}

